package com.ssm.service.impl;

import com.ssm.entity.Emp;
import com.ssm.entity.PageResult;
import com.ssm.entity.QueryObject;
import com.ssm.mapper.EmpMapper;
import com.ssm.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    @Transactional
    public void save(Emp emp) {
        if (emp.getName() == null) {
            throw new RuntimeException("部门名称不能为空");
        }
        empMapper.insert(emp);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        empMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(Emp emp) {
        empMapper.updateByPrimaryKey(emp);
    }

    @Override
    public Emp sqlById(Long id) {
        return empMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Emp> selAll() {
        return empMapper.selectAll();
    }

    @Override
    @Transactional
    public PageResult<Emp> query(QueryObject queryObject) {
        //查询总条数
        int i = empMapper.selForCount(queryObject);
        if (i == 0) {
            return new PageResult<Emp>(queryObject.getCurrentPage(), queryObject.getPageSize(), i, Collections.emptyList());
        }
        List<Emp> empList = empMapper.selForList(queryObject);
        return new PageResult<Emp>(queryObject.getCurrentPage(), queryObject.getPageSize(), i, empList);
    }

}
